package com.zx.mobile.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.map.dao.MerchantInfoDao;
import com.zx.map.dao.model.MerchantInfo;
import com.zx.map.dao.model.StoreInfo;
import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.MerchantOfflineConfigInfoService;
import com.zx.map.service.StoreInfoService;
import com.zx.map.service.model.MerchantInfoDetailModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.service.model.MerchantOfflineConfigInfoModel;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.mobile.utils.WebUtils;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 15-7-5 Time: 下午8:09 To
 * change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/shop")
public class MerchantController {

	@Autowired
	StoreInfoService storeInfoService;
	@Autowired
	MerchantInfoService merchantInfoService;
	@Autowired
	MerchantInfoDao merchantInfoDao;
	@Autowired
	MerchantOfflineConfigInfoService merchantOfflineConfigInfoService;
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(MerchantController.class);

	/**
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/find")
	@ResponseBody
	public Map find(@RequestBody MerchantInfoModel model, HttpServletRequest request) {
		try {

			List<MerchantInfo> list = merchantInfoService.findByName(model);
			return CommonResponseUtils.successResponse(list);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/bind")
	@ResponseBody
	public Map bind(@RequestBody MerchantInfoModel model, HttpServletRequest request) {
		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);

			if (model.getMerchantId() == null) {
				throw new Exception("请输入绑定id");
			}

			if (mUserDomain.getMerchantInfoDetailModel().getSysAccount().equals(model.getMerchantId())) {
				throw new Exception("不能绑定自己");
			}

			if (merchantInfoService.bind(mUserDomain.getMerchantInfoDetailModel().getMerchantId(),
					model.getMerchantId()) > 0) {
				return CommonResponseUtils.successResponse();
			} else {
				throw new Exception("用户不存在");
			}

		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/unbind")
	@ResponseBody
	public Map unbind(@RequestBody MerchantInfoModel model, HttpServletRequest request) {
		try {
			return CommonResponseUtils.successResponse(merchantInfoService.unbind(model.getMerchantId()));

		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/bindapprove")
	@ResponseBody
	public Map bindapprove(HttpServletRequest request) {
		try {
			int approve = Integer.parseInt(request.getParameter("approve"));
			String id = request.getParameter("id");
			merchantInfoService.approve(id, approve);
			return CommonResponseUtils.successResponse();
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/listdistributeshop")
	@ResponseBody
	public Map listdistributeshop(HttpServletRequest request) {
		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);
			List<MerchantInfo> list = merchantInfoService
					.findByParent(mUserDomain.getMerchantInfoDetailModel().getMerchantId());
			List<MerchantOfflineConfigInfoModel> l = new ArrayList<MerchantOfflineConfigInfoModel>();
			for (MerchantInfo m : list) {
				StoreInfo si = storeInfoService.find(m.getMerchantId());
				MerchantOfflineConfigInfoModel mi = new MerchantOfflineConfigInfoModel();
				mi.setMerchantId(m.getMerchantId());
				mi.setLegalImg(si.getStoreOutImg());
				mi.setApproved(m.getParentApproved());
				mi.setMerchantName(si.getStoreName());
				l.add(mi);
			}
			return CommonResponseUtils.successResponse(l);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/parentshop")
	@ResponseBody
	public Map parentshop(HttpServletRequest request) {
		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);
			List<MerchantInfo> list = null;
			list = merchantInfoService.getById(mUserDomain.getMerchantInfoDetailModel().getMerchantId());
			MerchantInfoDetailModel mm = new MerchantInfoDetailModel();
			if (list.get(0).getParentMerchantId() != null) {
				mm = merchantInfoService.getMerchantDetail(list.get(0).getParentMerchantId());
				StoreInfo si = storeInfoService.find(list.get(0).getParentMerchantId());
				mm.setBussinessImg(si.getStoreOutImg());
				mm.setMerchantName(si.getStoreName());
				mm.setParentApproved(list.get(0).getParentApproved());
			}
			return CommonResponseUtils.successResponse(mm);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/getBranchMoney")
	@ResponseBody
	public Map getBranchMoney(@RequestBody Map params, HttpServletRequest request) {
		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);

			Map<String, Object> result = merchantInfoService.getBranchMoney(
					mUserDomain.getMerchantInfoDetailModel().getMerchantId(),
					params.isEmpty() ? "" : (String) params.get("date"));
			return CommonResponseUtils.successResponse(result);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}
}
